
**********************************************************************************************************
* This dofile produces the figures and tables in the Online Appendix of
* Love Thy (Elected) Neighbor? Residential segregation, political representation and local public goods
* by Oskari Harjunen, Tuukka Saarimaa and Janne Tukiainen
* Stata/MP 17.0
**********************************************************************************************************

*Pahts
global results "C:\Users\saarimt1\Dropbox\Love thy politician\Results\"
global data "C:\Users\saarimt1\Data\HST\journal"

*ssc install reghdfe
*ssc install eventstudyinteract

**************************************************************************************************************
* Figure A1: Number of elementary schools and geography of school closures.
**************************************************************************************************************

use "$data\schools_pupils_2005_2017.dta", clear

la var pup_per_sch "Mean school size"
la var nschools "Number of elementary schools"

twoway bar nschools tilv, barwidth(0.4) ytitle(Number of elementary schools) xtitle(Year) ///
ylabel(1000(500)3500) xlab(2005(2)2017) graphregion(color(white)) ///
xline(2008.5) xline(2012.5) xline(2016.5) ///
|| scatter pup_per_sch tilv, msymbol(D) mcolor(black) connect(l) lcolor(black) yaxis(2) ylabel(50(50)200,axis(2))

graph export "$results\Figure1b.png", as(png) replace

*map created using QGIS

**************************************************************************************************************
* Figure A2: Polling districts and zipcodes in the municipality of Kuopio in 2019.
**************************************************************************************************************

*map created using QGIS


**************************************************************************************************************
* Figure A3: Municipalities with close elections.
**************************************************************************************************************

*map created using QGIS


**************************************************************************************************************
* Figure A4: Effect of school closures on residential sorting, small schools using Imai et al. (2021) method.
**************************************************************************************************************

global data "C:/Users/saarimt1/Dropbox/Love thy politician/dofiles/Journal/JOP_replication/"

use "$data\DID_est_Imai_et_al_replication.dta", clear

destring , replace

*create CI:s for all
foreach i in l_hi l_mi l_lo l_all s_hi s_mi s_lo s_all{
	gen `i'_low=est_`i'-(1.96*sd_`i')
	gen `i'_upp_=est_`i'+(1.96*sd_`i')
}

order time x est_l_hi sd_l_hi l_hi* 

global figspecs "xtitle("Years to closure") xline(-1, lcolor(red)) yline(0, lcolor(red)) xlabel(-3(1)3) ylabel(-0.08(0.04)0.08) ytitle("") graphregion(color(white))"

twoway (scatter est_s_lo x, msymbol(O) mcolor(black)) (rcap s_lo_low s_lo_upp x, connect(l) lpattern(solid) lcolor(black)), $figspecs subtitle("Log deciles 1-2") legend(off)
graph save "$results\small_log_1_2_rob.gph", replace

twoway (scatter est_s_mi x, msymbol(O) mcolor(black)) (rcap s_mi_low s_mi_upp x, connect(l) lpattern(solid) lcolor(black)), $figspecs subtitle("Log deciles 3-8") legend(off)
graph save "$results\small_log_3_8_rob.gph", replace

twoway (scatter est_s_hi x, msymbol(O) mcolor(black)) (rcap s_hi_low s_hi_upp x, connect(l) lpattern(solid) lcolor(black)), $figspecs subtitle("Log deciles 9-10") legend(off)
graph save "$results\small_log_9_10_rob.gph", replace

twoway (scatter est_s_lo x, msymbol(O) mcolor(black)) (rcap s_lo_low s_lo_upp x, connect(l) lpattern(solid) lcolor(black)), $figspecs subtitle("Log population") legend(off)
graph save "$results\small_logpop_rob.gph", replace

graph combine "$results\small_logpop_rob.gph" "$results\small_log_1_2_rob.gph" "$results\small_log_3_8_rob.gph" "$results\small_log_9_10_rob.gph", graphregion(color(white)) xsize(5) ysize(5)
graph export "$results\small_school_rob.png", replace


**************************************************************************************************************
* Figure A5: Effect of school closures on residential sorting, large schools using Imai et al. (2021) method.
**************************************************************************************************************

*create CI:s for all
foreach i in l_hi l_mi l_lo l_all s_hi s_mi s_lo s_all{
	gen `i'_low=est_`i'-(1.96*sd_`i')
	gen `i'_upp_=est_`i'+(1.96*sd_`i')
}

order time x est_l_hi sd_l_hi l_hi* 

global figspecs "xtitle("Years to closure") xline(-1, lcolor(red)) yline(0, lcolor(red)) xlabel(-3(1)3) ylabel(-0.08(0.04)0.08) ytitle("") graphregion(color(white))"

twoway (scatter est_l_lo x, msymbol(O) mcolor(black)) (rcap l_lo_low l_lo_upp x, connect(l) lpattern(solid) lcolor(black)), $figspecs subtitle("Log deciles 1-2") legend(off)
graph save "$results\large_log_1_2_rob.gph", replace

twoway (scatter est_l_mi x, msymbol(O) mcolor(black)) (rcap l_mi_low l_mi_upp x, connect(l) lpattern(solid) lcolor(black)), $figspecs subtitle("Log deciles 3-8") legend(off)
graph save "$results\large_log_3_8_rob.gph", replace

twoway (scatter est_l_hi x, msymbol(O) mcolor(black)) (rcap l_hi_low l_hi_upp x, connect(l) lpattern(solid) lcolor(black)), $figspecs subtitle("Log deciles 9-10") legend(off)
graph save "$results\large_log_9_10_rob.gph", replace

twoway (scatter est_l_lo x, msymbol(O) mcolor(black)) (rcap l_lo_low l_lo_upp x, connect(l) lpattern(solid) lcolor(black)), $figspecs subtitle("Log population") legend(off)
graph save "$results\large_logpop_rob.gph", replace

graph combine "$results\large_logpop_rob.gph" "$results\large_log_1_2_rob.gph" "$results\large_log_3_8_rob.gph" "$results\large_log_9_10_rob.gph", graphregion(color(white)) xsize(5) ysize(5)
graph export "$results\large_school_rob.png", replace


*****************************************************************************************************************
* Figure A6: Sensitivity of effect of school closures on residential sorting with respect to school size cutoff.
*****************************************************************************************************************

use "$data\zipcode_sorting.dta", clear

*nearest neighbor matching (nn = 2), get neighbors
nnmatch tunn closed_sch opp16 logpop if year==2013, tc(att) m(2) keep(nn_zip_schools_temp) replace
gen tunn_0=tunn

*check match data and create weights
preserve
use nn_zip_schools_temp, clear
unique tunn
unique tunn_0
bysort tunn_0: egen weight=count(tunn_0)
sort tunn_0
save nn_zip_schools, replace
restore

*merge neighbors
*nn_zip_schools includes only treated schools and their nearest neighbors
*neighbors school id's are in tunn_0, thus merging with tunn_0 and keeping the ones that merge keeps
*only the schools that were chosen as nearest neighbors using nnmatch
merge m:m tunn_0 using nn_zip_schools.dta

*keep treated and nn controls
gen control=(_merge==3)
gen estsample=(closed_sch==1 | control==1)
keep if estsample==1

replace weight=1 if missing(weight)
expand weight

gen never_closed=(closeyear==.)

la var _Ttimetotre_1 "t = -3"
la var _Ttimetotre_2 "t = -2"
la var _Ttimetotre_4 "t = 0"
la var _Ttimetotre_5 "t = 1"
la var _Ttimetotre_6 "t = 2"
la var _Ttimetotre_7 "t = 3"

cd "C:\Users\saarimt1\Dropbox\Love thy politician\results\"

foreach i of numlist 60 70 80 100  {
	eventstudyinteract log_9_10 _Ttimetotre_* if schoolnumb==1 & opp16>=`i', cohort(closeyear) 	control_cohort(never_closed) absorb(i.zipcode i.year) vce(cluster zipcode)

	preserve
	matrix C = e(b_iw)
	matrix list C

	mata st_matrix("A",sqrt(st_matrix("e(V_iw)")))
	matrix C = C \ A
	matrix list C
	mat event = C'
	matrix list event
	svmat event

	keep event*
	replace event1 = 0 in 7
	replace event2 = 0 in 7
	drop if missing(event1)

	gen temp=_n
	gen x=0
	replace x=-3 if temp==1
	replace x=-2 if temp==2
	replace x=-1 if temp==7
	replace x=0 if temp==3
	replace x=1 if temp==4
	replace x=2 if temp==5
	replace x=3 if temp==6
	sort x
	rename event1 coef
	rename event2 se
	mkmat coef se, matrix(X)
	matrix Y`i' = X'
	matrix list Y`i'
	
	coefplot matrix(Y`i'[1]), se(Y`i'[2]) vertical xtitle("Years to closure") xline(3, lcolor(red)) yline(0, lcolor(red)) ylabel(-0.08(0.04)0.08) ytitle("") graphregion(color(white)) coeflabels(r1 = "-3" r2 = "-2" r3 = "-1" r4 = "0" r5 ="1" r6 ="2" r7 ="3") mcolor(black) ciopts(lcolor(black)) subtitle(At least `i' pupils)
	graph save "log_9_10_`i'.gph", replace

	restore
}

graph combine "log_9_10_60.gph" "log_9_10_70.gph" "log_9_10_80.gph" "log_9_10_100.gph", graphregion(color(white)) xsize(5) ysize(5)
graph export "large_school_robustness.png", replace


*********************************************************************************************
* Table A1: Descriptive statistics for neighborhood representation measures..
*********************************************************************************************

use "$data\1km_grids_2012_estimation.dta", clear
tabstat candsha elecd elecsha if encrypted_inc==0, stat(mean sd count)

use "$data\zipcode_2012_estimation.dta", clear
tabstat candsha elecd elecsha if encrypted==0 & diffmun==0 & zipnumb>1, stat(mean sd count)


*********************************************************************************************
* Table A2: Political representation and socio-economic structure of neighborhoods (grid).
*********************************************************************************************

use "$data\1km_grids_2012_estimation.dta", clear

sum encrypted_inc

la var stdincomepc "Mean income"
la var educmscpc "Share highly educated"

est clear
reghdfe candsha popsha1km stdincomepc if encrypted_inc==0, absorb(kunnro2013) cluster(kunnro2013)
est sto inc1
reghdfe elecd popsha stdincomepc if encrypted_inc==0, absorb(kunnro2013) cluster(kunnro2013)
est sto inc2
reghdfe elecsha popsha stdincomepc if encrypted_inc==0, absorb(kunnro2013) cluster(kunnro2013)
est sto inc3
global tab "se(%9.3f) b(%9.3f) r2(%9.3f) nogaps star(* 0.05 ** 0.01) mtitles order(_cons) label"
esttab inc*, $tab keep(stdincomepc)

est clear
reghdfe candsha popsha educmscpc if encrypted_inc==0, absorb(kunnro2013) cluster(kunnro2013)
est sto edu1
reghdfe elecd popsha educmscpc if encrypted_inc==0, absorb(kunnro2013) cluster(kunnro2013)
est sto edu2
reghdfe elecsha popsha educmscpc if encrypted_inc==0, absorb(kunnro2013) cluster(kunnro2013)
est sto edu3
global tab "se(%9.3f) b(%9.3f) r2(%9.3f) nogaps star(* 0.05 ** 0.01) mtitles order(_cons) label"
esttab edu*, $tab keep(educmscpc)
*outcome means:
tabstat candsha elecd elecsha if encrypted_inc==0, stat(mean)


**************************************************************************
* Table A3: Turnout and socio-economic structure of polling districts.
**************************************************************************

use "$data\polling_district_socio.dta", replace

la var pd_stdincpc "Mean income"
la var pd_msc_sha "Share highly-educated"

est clear
reghdfe turnout pd_stdincpc pd_pop, absorb(kuntaidelec) cluster(kuntaidelec)
est sto pd1
reghdfe turnout pd_msc_sha pd_pop, absorb(kuntaidelec) cluster(kuntaidelec)
est sto pd2
global tab "se(%9.3f) b(%9.3f) r2(%9.3f) nogaps star(* 0.05 ** 0.01) mtitles order(_cons) label"
esttab pd*, $tab keep(pd_stdincpc pd_msc_sha)


************************************************************************************
* Table A4: Candidates' vote shares from own polling district.
*************************************************************************************

use "$data\EST_pollingdistrict_level_2012.dta", clear

la var d_oma_aa "Own district"
la var doma_restime "Own district x residence spell"
la var doma_inc "Own district x incumbent"
la var similarity "Voter similarity"
la var ownpartyvotesha "Own party vote share"

* Median number of polling dist in municipalities
sum n_aanestysa if muntemp==1, d // med = 4

est clear
reghdfe vsha_poldist100 d_oma_aa if n_aanestysa<=4 & n_aanestysa>1, absorb(hetu) clu(kuntaid)
est sto reg1
reghdfe vsha_poldist100 d_oma_aa if n_aanestysa>4, absorb(hetu) clu(kuntaid)
est sto reg2
reghdfe vsha_poldist100 d_oma_aa doma_restime doma_inc ownpartyvotesha similarity if n_aanestysa<=4 & n_aanestysa>1, absorb(hetu) clu(kuntaid)
est sto reg3
reghdfe vsha_poldist100 d_oma_aa doma_restime doma_inc ownpartyvotesha similarity if n_aanestysa>4, absorb(hetu) clu(kuntaid)
est sto reg4
global tab "se(%9.3f) b(%9.3f) r2(%9.2f) nogaps star(* 0.05 ** 0.01) mtitles order(_cons) label"
esttab reg1 reg2 reg3 reg4, $tab keep(d_oma_aa doma_restime doma_inc ownpartyvotesha similarity) 
*outcome mean
tabstat vsha_poldist100 if n_aanestysa<=4 & n_aanestysa>1, stat(mean)
tabstat vsha_poldist100 if n_aanestysa>4, stat(mean)


*************************************************************************************
* Table A5: Covariate balance at candidate level.
*************************************************************************************

use "$data\causal_estimation_data.dta", clear

global cons1 "if nodup1==1"
global cons2 "if nodup2==1"

gen majority=(sch_seatsha>0.5)

global depvars "age male child12 child12numb incumbent student unemployed entrepreneur highprofessional dist_sch pupils sch_seatsha sch_repres majority"

la var elected "Elected"
la var age "Age"
la var male "Male (0/1)"
la var child12 "Children (0/1)"
la var incumbent "Incumbent (0/1)"
la var child12numb "Number of children"
la var student "Student (0/1)"
la var entrepreneur "Entrepreneur (0/1)"
la var unemployed "Unemployed (0/1)"
la var highprofessional "Managerial employee (0/1)"
la var sch_seatsha "School seat share"
la var dist_sch_dif "Change in school distance (km)"
la var dist_sch "Distance to school (km)"
la var pupils "Number of pupils in school"
la var sch_repres "School has representation (0/1)"
la var sch_votesha "School vote share"
la var majority "School has seat majority"

*Lottery
est clear
foreach var of varlist age male child12 child12numb incumbent student unemployed entrepreneur highprofessional dist_sch pupils sch_seatsha sch_repres majority {
	reg `var' elected $cons1, cluster(kuntaidelec)
	est sto reg_`var'
	esttab reg_`var'
}

global tab "se(%9.3f) b(%9.3f) r2(%9.3f) nogaps star(* 0.05 ** 0.01) mtitles order(_cons) label"

esttab reg_age reg_male reg_child12 reg_child12numb reg_incumbent reg_student reg_unemployed reg_entrepreneur, $tab
esttab reg_highprofessional reg_dist_sch reg_pupils reg_sch_seatsha reg_sch_repres reg_majority, $tab

*One vote margin
est clear
foreach var of varlist age male child12 child12numb incumbent student unemployed entrepreneur highprofessional dist_sch pupils sch_seatsha sch_repres majority {
	reg `var' elected $cons2, cluster(kuntaidelec)
	est sto reg_`var'
}

global tab "se(%9.3f) b(%9.3f) r2(%9.3f) nogaps star(* 0.05 ** 0.01) mtitles order(_cons) label"

esttab reg_age reg_male reg_child12 reg_child12numb reg_incumbent reg_student reg_unemployed reg_entrepreneur, $tab
esttab reg_highprofessional reg_dist_sch reg_pupils reg_sch_seatsha reg_sch_repres reg_majority, $tab


**********************************************************************************************
* Table A6: Means and standard deviations of candidate characteristics in different samples.
**********************************************************************************************

use "$data\causal_estimation_data.dta", clear

global depvars "age male child12 child12numb incumbent student unemployed entrepreneur highprofessional"

la var elected "Elected"
la var age "Age"
la var male "Male (0/1)"
la var child12 "Children (0/1)"
la var incumbent "Incumbent (0/1)"
la var child12numb "Number of children"
la var student "Student (0/1)"
la var entrepreneur "Entrepreneur (0/1)"
la var unemployed "Unemployed (0/1)"
la var highprofessional "Managerial employee (0/1)"

tabstat $depvars if nodup1==1, stat(mean sd count)
tabstat $depvars if nodup2==1, stat(mean sd count)
tabstat $depvars if elected==0, stat(mean sd count)
tabstat $depvars if elected==1, stat(mean sd count)


****************************************************************************************
* Table A7: Descriptive statistics for municipalities with and without close elections 
****************************************************************************************

use "$data\causal_estimation_data.dta", clear
drop muntemp

*municipalities
*lottery
preserve
keep if nodup1==1 
bysort elecyear kuntaidelec: gen muntemp=1 if _n==1
keep if muntemp==1
tabstat munpop under15sha incomepc turnout counsize mun_schools, c(s) stat(mean sd n)
restore

*one vote margin
preserve
keep if nodup2==1 
bysort elecyear kuntaidelec: gen muntemp=1 if _n==1
keep if muntemp==1
tabstat munpop under15sha incomepc turnout counsize mun_schools, c(s) stat(mean sd n)
restore

*other municipalities
preserve
keep if nodup1!=1 & nodup2!=1 
bysort elecyear kuntaidelec: gen muntemp=1 if _n==1
keep if muntemp==1
tabstat munpop under15sha incomepc turnout counsize mun_schools, c(s) stat(mean sd n)
restore


*************************************************************************************
* Table A8: Effects of representation on school closure (non-movers).
*************************************************************************************

use "$data\causal_estimation_data.dta", replace

global ctrl_sch "pupils"
global ctrl_cand "age male child12 incumbent student unemployed entrepreneur highprofessional i.party"

la var elected "Elected"

*Panel A: Lottery
global cons "if nodup1==1 & moved==0"
est clear
reg closed_sch elected $cons, clu(kuntaidelec)
est sto reg1
reg closed_sch elected $ctrl_sch $cons, clu(kuntaidelec)
est sto reg2
reg closed_sch elected $ctrl_sch $ctrl_cand $cons, clu(kuntaidelec)
est sto reg3
reg closed_sch elected $ctrl_sch $ctrl_cand i.elecyear $cons, clu(kuntaidelec)
est sto reg4
global tab "se(%9.3f) b(%9.3f) r2(%9.3f) nogaps star(* 0.05 ** 0.01) mtitles order(_cons) label"
esttab reg1 reg2 reg3 reg4, $tab keep(_cons elected)

*Panel B: One vote margin
global cons "if nodup2==1 & moved==0"
est clear
reg closed_sch elected $cons, clu(kuntaidelec)
est sto reg1
reg closed_sch elected $ctrl_sch $cons, clu(kuntaidelec)
est sto reg2
reg closed_sch elected $ctrl_sch $ctrl_cand $cons, clu(kuntaidelec)
est sto reg3
reg closed_sch elected $ctrl_sch $ctrl_cand i.elecyear $cons, clu(kuntaidelec)
est sto reg4
global tab "se(%9.3f) b(%9.3f) r2(%9.3f) nogaps star(* 0.05 ** 0.01) mtitles order(_cons) label"
esttab reg1 reg2 reg3 reg4, $tab keep(_cons elected)


****************************************************************************************
* Table A9: Effect of representation on school closure (municipalities with closures).
****************************************************************************************

use "$data\causal_estimation_data.dta", replace

global ctrl_sch "pupils"
global ctrl_cand "age male child12 incumbent student unemployed entrepreneur highprofessional i.party"

la var elected "Elected"

*Panel A: Lottery
global cons "if nodup1==1 & mun_closed>0"
est clear
reg closed_sch elected $cons, clu(kuntaidelec)
est sto reg1
reg closed_sch elected $ctrl_sch $cons, clu(kuntaidelec)
est sto reg2
reg closed_sch elected $ctrl_sch $ctrl_cand $cons, clu(kuntaidelec)
est sto reg3
reg closed_sch elected $ctrl_sch $ctrl_cand i.elecyear $cons, clu(kuntaidelec)
est sto reg4
global tab "se(%9.3f) b(%9.3f) r2(%9.3f) nogaps star(* 0.05 ** 0.01) mtitles order(_cons) label"
esttab reg1 reg2 reg3 reg4, $tab keep(_cons elected)

*one vote margin
global cons "if nodup2==1 & mun_closed>0"
est clear
reg closed_sch elected $cons, clu(kuntaidelec)
est sto reg1
reg closed_sch elected $ctrl_sch $cons, clu(kuntaidelec)
est sto reg2
reg closed_sch elected $ctrl_sch $ctrl_cand $cons, clu(kuntaidelec)
est sto reg3
reg closed_sch elected $ctrl_sch $ctrl_cand i.elecyear $cons, clu(kuntaidelec)
est sto reg4
global tab "se(%9.3f) b(%9.3f) r2(%9.3f) nogaps star(* 0.05 ** 0.01) mtitles order(_cons) label"
esttab reg1 reg2 reg3 reg4, $tab keep(_cons elected)


************************************************************************************************
* Table A10: Effect heterogeneity with respect to being from the largest party in the council.
************************************************************************************************

use "$data\causal_estimation_data.dta", clear

global ctrl_sch "pupils"
global ctrl_cand "age male child12 incumbent student unemployed entrepreneur highprofessional i.party"

la var elected "Elected"

global cons1 "if nodup1==1"
global cons2 "if nodup2==1"
est clear
reg closed_sch elected $ctrl_sch $ctrl_cand i.elecyear $cons1 & largestparty==1, clu(kuntaidelec)
est sto largest1
reg closed_sch elected $ctrl_sch $ctrl_cand i.elecyear $cons1 & largestparty==0, clu(kuntaidelec)
est sto not1
reg closed_sch elected $ctrl_sch $ctrl_cand i.elecyear $cons2 & largestparty==1, clu(kuntaidelec)
est sto largest2
reg closed_sch elected $ctrl_sch $ctrl_cand i.elecyear $cons2 & largestparty==0, clu(kuntaidelec)
est sto not2
global tab "se(%9.3f) b(%9.3f) r2(%9.3f) nogaps star(* 0.05 ** 0.01) mtitles order(_cons) label"
esttab largest1 not1 largest2 not2, $tab keep(_cons elected)

*outcome mean in control group
est clear
reg closed_sch elected $cons1 & largestparty==1, clu(kuntaidelec)
est sto largest1
reg closed_sch elected $cons1 & largestparty==0, clu(kuntaidelec)
est sto not1
reg closed_sch elected $cons2 & largestparty==1, clu(kuntaidelec)
est sto largest2
reg closed_sch elected $cons2 & largestparty==0, clu(kuntaidelec)
est sto not2
global tab "se(%9.3f) b(%9.3f) r2(%9.3f) nogaps mtitles order(_cons) label nostar"
esttab largest1 not1 largest2 not2, $tab keep(_cons) 


**********************************************************************
* Table A11: Effect heterogeneity with respect to council size.
**********************************************************************

use "$data\causal_estimation_data.dta", clear

global ctrl_sch "pupils"
global ctrl_cand "age male child12 incumbent student unemployed entrepreneur highprofessional i.party"

la var elected "Elected"

sum councilsize if muntemp==1, d //27

global cons1 "if nodup1==1"
global cons2 "if nodup2==1"
est clear
reg closed_sch elected $ctrl_sch $ctrl_cand i.elecyear $cons1 & councilsize<=27, clu(kuntaidelec)
est sto reg1
reg closed_sch elected $ctrl_sch $ctrl_cand i.elecyear $cons1 & councilsize>27, clu(kuntaidelec)
est sto reg2
reg closed_sch elected $ctrl_sch $ctrl_cand i.elecyear $cons2 & councilsize<=27, clu(kuntaidelec)
est sto reg3
reg closed_sch elected $ctrl_sch $ctrl_cand i.elecyear $cons2 & councilsize>27, clu(kuntaidelec)
est sto reg4
global tab "se(%9.3f) b(%9.3f) r2(%9.3f) nogaps star(* 0.05 ** 0.01) mtitles order(_cons) label"
esttab reg1 reg2 reg3 reg4, $tab keep(_cons elected)

*Control group outcome mean
est clear
reg closed_sch elected $cons1 & councilsize<=27, clu(kuntaidelec)
est sto reg1
reg closed_sch elected $cons1 & councilsize>27, clu(kuntaidelec)
est sto reg2
reg closed_sch elected $cons2 & councilsize<=27, clu(kuntaidelec)
est sto reg3
reg closed_sch elected $cons2 & councilsize>27, clu(kuntaidelec)
est sto reg4
global tab "se(%9.3f) b(%9.3f) r2(%9.3f) nogaps mtitles order(_cons) label nostar"
esttab reg1 reg2 reg3 reg4, $tab keep(_cons) 


***************************************************************
* Table A12: Effect heterogeneity with respect to school size.
***************************************************************

use "$data\causal_estimation_data.dta", clear

global ctrl_sch "pupils"
global ctrl_cand "age male child12 incumbent student unemployed entrepreneur highprofessional i.party"

sum pupils pupilsha if nodup1==1, d
sum pupils pupilsha if nodup2==1, d

gen medpupils1=(pupils>92)
gen medpupils2=(pupils>76)

gen medpupilsha1=(pupilsha>0.1397569 )
gen medpupilsha2=(pupilsha>0.1267861)

*absolute school size
global cons1 "if nodup1==1"
global cons2 "if nodup2==1"
est clear
reg closed_sch elected i.elecyear $ctrl_sch $ctrl_cand $cons1 & medpupils1==0, clu(kuntaidelec)
est sto reg1
reg closed_sch elected i.elecyear $ctrl_sch $ctrl_cand $cons1 & medpupils1==1, clu(kuntaidelec)
est sto reg2
reg closed_sch elected i.elecyear $ctrl_sch $ctrl_cand $cons2 & medpupils2==0, clu(kuntaidelec)
est sto reg3
reg closed_sch elected i.elecyear $ctrl_sch $ctrl_cand $cons2 & medpupils2==1, clu(kuntaidelec)
est sto reg4
global tab "se(%9.3f) b(%9.3f) r2(%9.3f) nogaps star(* 0.05 ** 0.01) mtitles order(_cons) label"
esttab reg*, $tab keep(_cons elected)

*control group outcome means
global cons1 "if nodup1==1"
global cons2 "if nodup2==1"
est clear
reg closed_sch elected $cons1 & medpupils1==0, clu(kuntaidelec)
est sto reg1
reg closed_sch elected $cons1 & medpupils1==1, clu(kuntaidelec)
est sto reg2
reg closed_sch elected $cons2 & medpupils2==0, clu(kuntaidelec)
est sto reg3
reg closed_sch elected $cons2 & medpupils2==1, clu(kuntaidelec)
est sto reg4
global tab "se(%9.3f) b(%9.3f) r2(%9.3f) nogaps mtitles order(_cons) label nostar"
esttab reg1 reg2 reg3 reg4, $tab keep(_cons)

*relative school size
est clear
reg closed_sch elected i.elecyear $ctrl_sch $ctrl_cand $cons1 & medpupilsha1==0, clu(kuntaidelec)
est sto reg1
reg closed_sch elected i.elecyear $ctrl_sch $ctrl_cand $cons1 & medpupilsha1==1, clu(kuntaidelec)
est sto reg2
reg closed_sch elected i.elecyear $ctrl_sch $ctrl_cand $cons2 & medpupilsha2==0, clu(kuntaidelec)
est sto reg3
reg closed_sch elected i.elecyear $ctrl_sch $ctrl_cand $cons2 & medpupilsha2==1, clu(kuntaidelec)
est sto reg4
global tab "se(%9.3f) b(%9.3f) r2(%9.3f) nogaps star(* 0.05 ** 0.01) mtitles order(_cons) label"
esttab reg*, $tab keep(_cons elected)

*control group outcome means
est clear
reg closed_sch elected $cons1 & medpupilsha1==0, clu(kuntaidelec)
est sto reg1
reg closed_sch elected $cons1 & medpupilsha1==1, clu(kuntaidelec)
est sto reg2
reg closed_sch elected $cons2 & medpupilsha2==0, clu(kuntaidelec)
est sto reg3
reg closed_sch elected $cons2 & medpupilsha2==1, clu(kuntaidelec)
est sto reg4
global tab "se(%9.3f) b(%9.3f) r2(%9.3f) nogaps mtitles order(_cons) label nostar"
esttab reg1 reg2 reg3 reg4, $tab keep(_cons)


********************************************************************
* Table A13: Effect heterogeneity with respect to school distance.
********************************************************************

use "$data\causal_estimation_data.dta", clear

global ctrl_sch "pupils"
global ctrl_cand "age male child12 incumbent student unemployed entrepreneur highprofessional i.party"

la var elected "Elected"

sum dist_nnsch if nodup1==1, d
sum dist_nnsch if nodup2==1, d

gen med_nnsch1=(dist_nnsch>4.149368)
gen med_nnsch2=(dist_nnsch>4.017214)

*NOTE: mistake in one vote margin sample: wrong variable was used for the sample split
global cons1 "if nodup1==1"
global cons2 "if nodup2==1"
est clear
reg closed_sch elected i.elecyear $ctrl_sch $ctrl_cand $cons1 & med_nnsch1==0, clu(kuntaidelec)
est sto reg1
reg closed_sch elected i.elecyear $ctrl_sch $ctrl_cand $cons1 & med_nnsch1==1, clu(kuntaidelec)
est sto reg2
reg closed_sch elected i.elecyear $ctrl_sch $ctrl_cand $cons2 & med_nnsch2==0, clu(kuntaidelec)
est sto reg3
reg closed_sch elected i.elecyear $ctrl_sch $ctrl_cand $cons2 & med_nnsch2==1, clu(kuntaidelec)
est sto reg4
global tab "se(%9.3f) b(%9.3f) r2(%9.3f) nogaps star(* 0.05 ** 0.01) mtitles order(_cons) label"
esttab reg*, $tab keep(_cons elected)

*control group baselines
global cons1 "if nodup1==1"
global cons2 "if nodup2==1"
est clear
reg closed elected $cons1 & med_nnsch1==0, clu(kuntaidelec)
est sto reg1
reg closed elected $cons1 & med_nnsch1==1, clu(kuntaidelec)
est sto reg2
reg closed elected $cons2 & med_nnsch2==0, clu(kuntaidelec)
est sto reg3
reg closed elected $cons2 & med_nnsch2==1, clu(kuntaidelec)
est sto reg4
global tab "se(%9.3f) b(%9.3f) r2(%9.3f) nogaps mtitles order(_cons) label nostar"
esttab reg1 reg2 reg3 reg4, $tab keep(_cons)


*****************************************************************************************************
* Table A14: Effect heterogeneity with respect to candidates having children under the age of 12.
*****************************************************************************************************

use "$data\causal_estimation_data.dta", clear

global ctrl_sch "pupils"
global ctrl_cand "age male child12 incumbent student unemployed entrepreneur highprofessional i.party"

la var elected "Elected"

global cons1 "if nodup1==1"
global cons2 "if nodup2==1"

*NOTE: mistake in R2's?
est clear
reg closed_sch elected i.elecyear $ctrl_sch $ctrl_cand $cons1 & child12==0, clu(kuntaidelec)
est sto reg1
reg closed_sch elected i.elecyear $ctrl_sch $ctrl_cand $cons1 & child12==1, clu(kuntaidelec)
est sto reg2
reg closed_sch elected i.elecyear $ctrl_sch $ctrl_cand $cons2 & child12==0, clu(kuntaidelec)
est sto reg3
reg closed_sch elected i.elecyear $ctrl_sch $ctrl_cand $cons2 & child12==1, clu(kuntaidelec)
est sto reg4
global tab "se(%9.3f) b(%9.3f) r2(%9.3f) nogaps star(* 0.05 ** 0.01) mtitles order(_cons) label"
esttab reg*, $tab keep(_cons elected)

*control group baselines
est clear
reg closed_sch elected $cons1 & child12==0, clu(kuntaidelec)
est sto reg1
reg closed_sch elected $cons1 & child12==1, clu(kuntaidelec)
est sto reg2
reg closed_sch elected $cons2 & child12==0, clu(kuntaidelec)
est sto reg3
reg closed_sch elected $cons2 & child12==1, clu(kuntaidelec)
est sto reg4
global tab "se(%9.3f) b(%9.3f) r2(%9.3f) nogaps mtitles order(_cons) label nostar"
esttab reg1 reg2 reg3 reg4, $tab keep(_cons)


*****************************************************************************************************
* Table A15: Descriptive statistics for schools and zipcodes in the residential re-sorting analysis.
*****************************************************************************************************

use "$data\zipcode_sorting.dta", clear

*nearest neighbor matching (nn = 2), get neighbors
nnmatch tunn closed_sch opp16 logpop if year==2013, tc(att) m(2) keep(nn_zip_schools_temp) replace
gen tunn_0=tunn

*check match data and create weights
preserve
use nn_zip_schools_temp, clear
unique tunn
unique tunn_0
bysort tunn_0: egen weight=count(tunn_0)
sort tunn_0
save nn_zip_schools, replace
restore

*merge neighbors
merge m:m tunn_0 using nn_zip_schools.dta

*keep treated and nn controls
gen control=(_merge==3)
gen estsample=(closed_sch==1 | control==1)
*keep if estsample==1

replace weight=1 if missing(weight)
expand weight

gen never_closed=(closeyear==.)

keep if year==2013
global depvars "opp16 he_vakiy hr_pi_tul hr_ke_tul hr_hy_tul ko_yl_kork"

la var opp16 "Number of pupils"
la var he_vakiy "Population"
la var hr_pi_tul "Residents in deciles 1 and 2"
la var hr_ke_tul "Residents in deciles 3-8"
la var hr_hy_tul "Residents in deciles 9 and 10"
la var ko_yl_kork "Highly-educated residents"

tabstat $depvars if closed_sch==0, stat(mean sd count)
tabstat $depvars if closed_sch==0 & estsample==1, stat(mean sd count)
tabstat $depvars if closed_sch==1, stat(mean sd count)

